package com.miaosha.ms_01.redis;

import com.alibaba.fastjson.JSON;
import com.miaosha.ms_01.prefix.KeyPrefix;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;

/**
 * @Description //TODO
 * @Author Administrator
 * @DATE 2020/9/13 19:56
 **/
@Service
public class JedisService {

    @Resource
    private JedisPool jedisPool;

    public <T> T get(KeyPrefix keyPrefix, String key, Class<T> cls) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPrefix() + key;
            String str = jedis.get(realKey);
            return strToBean(str, cls);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnToPool(jedis);
        }
        return null;
    }

    public <T> Boolean set(KeyPrefix keyPrefix, String key, T value, Class<T> cls) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String valueStr = beanToStr(value);
            String realKey = keyPrefix.getPrefix() + key;
            int expireTime = keyPrefix.getExpireTime();
            if (expireTime <= 0) {
                jedis.set(realKey, valueStr);
            } else {
                jedis.setex(realKey, expireTime, valueStr);
            }
            return true;
        } finally {
            returnToPool(jedis);
        }
    }

    public <T> Boolean exists(KeyPrefix keyPrefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPrefix() + key;
            return jedis.exists(realKey);
        } finally {
            returnToPool(jedis);
        }
    }

    public <T> Long incr(KeyPrefix keyPrefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPrefix() + key;
            return jedis.incr(realKey);
        } finally {
            returnToPool(jedis);
        }
    }


    public <T> Long desc(KeyPrefix keyPrefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = keyPrefix.getPrefix() + key;
            return jedis.decr(realKey);
        } finally {
            returnToPool(jedis);
        }
    }


    private void returnToPool(Jedis jedis) {
        jedis.close();
    }

    private <T> T strToBean(String str, Class<T> cls) {
        if (StringUtils.isEmpty(str) || cls == null) {
            return null;
        }
        if (cls == int.class || cls == Integer.class) {
            return (T) Integer.valueOf(str);
        } else if (cls == String.class) {
            return (T) str;
        } else if (cls == long.class || cls == Long.class) {
            return (T) Long.valueOf(str);
        } else {
            return JSON.toJavaObject(JSON.parseObject(str), cls);
        }
    }

    private <T> String beanToStr(T value) {
        if (value == null) {
            return null;
        }
        Class<?> clazz = value.getClass();
        if (clazz == int.class || clazz == Integer.class) {
            return "" + value;
        } else if (clazz == String.class) {
            return (String) value;
        } else if (clazz == long.class || clazz == Long.class) {
            return "" + value;
        } else {
            return JSON.toJSONString(value);
        }
    }
}
